package com.zf.mall.gateway.handler;

import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.zf.mall.common.response.ResponseBaseModel;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;

@Slf4j
public class SentinelFallbackHandler implements BlockRequestHandler {

	    @Override
	    public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
	        ServerHttpRequest request = serverWebExchange.getRequest();
	        
	        log.error("资源{} , 触发限流规则, 详情 : {} ", request.getPath(), throwable);
	        return ServerResponse.status(200)
	                .contentType(MediaType.APPLICATION_JSON)
	                .body(BodyInserters.fromValue(new ResponseBaseModel(20000, "当前系统访问人数太多,将进行访问限流,请稍后访问....")));
	    }
	}